package com.bjshiwei.zhengu.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.bean.HomeViewPointBean;
import com.bjshiwei.zhengu.config.ImageConfig;
import com.bjshiwei.zhengu.utils.DateFormatUtil;
import com.bjshiwei.zhengu.utils.VolleyRequest;

import java.util.List;

/**
 * Created by Administrator on 2017/2/16.
 */
public class HomeViewPointAdapter extends MyBaseAdapter{


    private List<HomeViewPointBean.ListBean>  mDatas;
    private Context mContext;
    private VolleyRequest mVolleyRequest;
    private DateFormatUtil mDateFormatUtil;

    public HomeViewPointAdapter(List datas, Context context) {
        super(context,datas);
        mContext = context;
        mDatas = datas;
        mVolleyRequest = VolleyRequest.getInstance(mContext);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(view==null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_home_viewpoint, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if(!TextUtils.isEmpty(mDatas.get(i).getImage())) {
            String image = mDatas.get(i).getImage();
            mVolleyRequest.getImage(image,viewHolder.mImageViewIcon, ImageConfig.DEFAULT_IMAGE,ImageConfig.ERROR_IMAGE);
        }
        if(!TextUtils.isEmpty(mDatas.get(i).getTitle())) {
            viewHolder.mTextViewBody.setText(mDatas.get(i).getTitle());
        }
        if(!TextUtils.isEmpty(String.valueOf(mDatas.get(i).getCreate_time()))) {
            long pub_time = mDatas.get(i).getCreate_time();
            mDateFormatUtil = DateFormatUtil.getInstance();
            String format = mDateFormatUtil.dateFromatToHour(pub_time);
            viewHolder.mTextViewTime.setText(format);
        }
        return view;
    }


    public class ViewHolder{
        private TextView mTextViewBody;
        private TextView mTextViewTime;
        private ImageView mImageViewIcon;
        public ViewHolder(View view){
            mTextViewBody = (TextView) view.findViewById(R.id.tv_viewpoint_body);
            mTextViewTime = (TextView) view.findViewById(R.id.tv_viewpoint_time);
            mImageViewIcon = (ImageView) view.findViewById(R.id.iv_home_viewpoint);
        }

    }
}
